﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.APP.APP_UI.Add_Form
{
    public partial class ICM_Master_Warehouse : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        IBLLMasterWarehouse bll = new MasterWarehouseBLL();
        IBLLTree bllTree = new TreeBLL();
        IBLLSysScope bllSysScope = new SysScopeBLL();
        IBLLHrmScope bllHrmScope = new HrmScopeBLL();
        IBLLMasterWarehouseConsume bllConsume = new MasterWarehouseConsumeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadScope();
                LoadJobSiteTree();
                LoadJobSiteConsumeGrid();
            }
        }

        public void LoadScope()
        {
            IList<SysScopeInfo> info = bllSysScope.GetSysScope();
            cbScope.DataSource = info;
            cbScope.DataBind();
        }

        public void LoadJobSiteTree()
        {
            IList<TreeInfo> info = bllTree.GetJobSiteTree();

            RadTreeView tree = (RadTreeView)cbJobSite.Items[0].FindControl("TreeViewJobSite");

            tree.DataSource = info;
            tree.DataBind();
        }

        public void LoadJobSiteConsumeGrid()
        {
            IList<MasterWarehouseConsumeInfo> infoListConsume = new List<MasterWarehouseConsumeInfo>();
            if (ViewState["Consume"] != null)
            {
                infoListConsume = (List<MasterWarehouseConsumeInfo>)ViewState["Consume"];
            }
            Grid_JobSite.DataSource = infoListConsume;
            GridGroupByExpression ge = new GridGroupByExpression(Grid_JobSite.Columns[1]);
            Grid_JobSite.MasterTableView.GroupByExpressions.Add(ge);
            Grid_JobSite.DataBind();

            cmdFac.GridCommand(panelList_JobSite, panelGrid_JobSite, panelCommand_JobSite);
        }

        protected void AddNew_JobSite_Click(object sender, EventArgs e)
        {
            IList<TreeInfo> infoList = bllTree.GetWarehouseConsumeJobSiteTree(null);
            JobsiteTree.DataSource = infoList;
            JobsiteTree.DataBind();
            JobsiteTree.ExpandAllNodes();

            if (ViewState["Consume"] != null)
            {
                foreach (RadTreeNode node in JobsiteTree.GetAllNodes())
                {
                    foreach (MasterWarehouseConsumeInfo info in (List<MasterWarehouseConsumeInfo>)ViewState["Consume"])
                    {
                        string[] all_value = node.Value.Split(new string[] { "-" }, StringSplitOptions.None);
                        if (all_value.Length == 2 && info.InfoJobSite.Seq == Convert.ToInt32(all_value[1]))
                        {
                            node.Checked = true;
                            break;
                        }
                    }
                }
            }
            cmdFac.AddNewCommand(panelList_JobSite, panelGrid_JobSite, panelCommand_JobSite);
        }

        protected void AddNewSave_JobSite_Click(object sender, EventArgs e)
        {
            IList<MasterWarehouseConsumeInfo> infoListConsume = new List<MasterWarehouseConsumeInfo>(); 
            ViewState["Consume"] = null;
            foreach (RadTreeNode node in JobsiteTree.GetAllNodes())
            {
                if (node.Checked)
                {
                    string[] all_value = node.Value.Split(new string[] { "-" }, StringSplitOptions.None);
                    if (all_value.Length == 2)
                    {
                        MasterWarehouseConsumeInfo info = new MasterWarehouseConsumeInfo();
                        HrmJobSiteInfo infoJobSite = new HrmJobSiteInfo();
                        SysScopeInfo infoScope = new SysScopeInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        infoJobSite.Seq = Convert.ToInt32(all_value[1]);
                        infoJobSite.Name = node.Text;
                        infoScope.Seq = Convert.ToInt32(node.ParentNode.Value);
                        infoScope.Name = node.ParentNode.Text;
                        infoJobSite.InfoScope = infoScope;
                        infoRecord.Created_by = cn.UserID;
                        infoRecord.Created_when = DateTime.Now;
                        info.InfoJobSite = infoJobSite;
                        info.InfoRecord = infoRecord;
                        infoListConsume.Add(info);
                    }
                }
            }
            ViewState["Consume"] = infoListConsume;
            LoadJobSiteConsumeGrid();
        }

        protected void Delete_JobSite_Click(object sender, EventArgs e)
        {
            int[] data_value = new int[Grid_JobSite.MasterTableView.GetSelectedItems().Length];
            int item = Grid_JobSite.MasterTableView.GetSelectedItems().Length;
            foreach (GridDataItem data in Grid_JobSite.MasterTableView.GetSelectedItems())
            {
                item = item - 1;
                data_value[item] = data.ItemIndex;
            }

            for (int i = 0; i < data_value.Length; i++)
            {
                ((List<MasterWarehouseConsumeInfo>)ViewState["Consume"]).RemoveAt(data_value[i]);
            }

            LoadJobSiteConsumeGrid();
        }

        protected void ShowGrid_JobSite_Click(object sender, EventArgs e)
        {
            LoadJobSiteConsumeGrid();
        }

        protected void Grid_JobSite_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadJobSiteConsumeGrid();
        }

        protected void finishButton_JobSite_Click(object sender, EventArgs e)
        {
            MasterWarehouseInfo info = new MasterWarehouseInfo();
            SysScopeInfo infoScope = new SysScopeInfo();
            HrmJobSiteInfo infoJobSite = new HrmJobSiteInfo();
            RecordInfo infoRecord = new RecordInfo();
            IList<MasterWarehouseConsumeInfo> infoListConsume = new List<MasterWarehouseConsumeInfo>();
            if (ViewState["Consume"] != null)
            {
                infoListConsume = (List<MasterWarehouseConsumeInfo>)ViewState["Consume"];
            }
            RadTreeView tree = (RadTreeView)cbJobSite.Items[0].FindControl("TreeViewJobSite");
            info.Name = txtName.Text;
            info.Descr = txtDescr.Text;
            infoScope.Seq = (cbScope.SelectedValue.Length == 0 ? null : (Nullable<int>)Convert.ToDouble(cbScope.SelectedValue));
            if (tree.SelectedNode != null)
            {
                string[] all_value = tree.SelectedNode.Value.Split(new string[] { "-" }, StringSplitOptions.None);
                infoJobSite.Seq = Convert.ToInt32(all_value[1]);
            } 
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoJobSite = infoJobSite;
            info.InfoScope = infoScope;
            info.InfoRecord = infoRecord;

            try
            {
                bll.Insert(info, infoListConsume);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }

            RadManager1.Alert(SystemInfo._InsertComplete);
            RadManager1.ResponseScripts.Add(@"CloseWindow();");
        }

        protected void clearFilter_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid_JobSite.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid_JobSite.MasterTableView.FilterExpression = string.Empty;
            LoadJobSiteConsumeGrid();
        }
    }
}
